import { Injectable, NestInterceptor, ExecutionContext, HttpException } from '@nestjs/common';
import { Observable } from 'rxjs';
import { tap } from 'rxjs/operators';
import * as jwt from 'jsonwebtoken';

@Injectable()
export class CommonInterceptor implements NestInterceptor {
  intercept(
    context: ExecutionContext,
    call$: Observable<any>,
  ): Observable<any> {
    const token = context.switchToHttp().getRequest().headers['token'];
    if(token === ''){
        throw new HttpException({msg:'登陆失效'},401);
    }
    var decoded = jwt.decode(token, {complete: true});
    if(decoded && decoded['paload'] && decoded['paload']){

    }
    console.log(decoded);
    return call$.pipe(
      tap(() => {
        // if (userName == '') {
        //   throw new HttpException({msg:'未登陆'},401);
        // }
      }),
    );
  }
}